using System;
using System.Collections.Generic;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Drawing;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CodeStory.Core;
using CodeStory.Core.ObjectEntity;
using CodeStory.Core.UI;
using Activa.CoolStorage;

namespace CodeStory.WebApplication.Modules.Core.Admin
{
	[NavigationPortlet("CodeStory_Core_Admin_CreatePage")]
	public partial class CreatePage : Portlet
	{
		protected override void OnInit(EventArgs e) {
			Load += new EventHandler(CreatePage_Load);
			ddlModules.SelectedIndexChanged += new EventHandler(ModuleDropDownList_SelectedIndexChanged);
			btnAdd.Click += new EventHandler(ButtonAdd_Click);
			base.OnInit(e);
		}

		void ButtonAdd_Click(object sender, EventArgs e) {
			PagePortletObject obj = PagePortletObject.New();
			obj.PagePortletID = Guid.NewGuid();
			obj.PageID = EditPageID;
			obj.PortletID = new Guid(ddlPortlets.SelectedValue);
			obj.PaneName = ddlPanes.SelectedValue;
			obj.Save();
			CreateEditPage();
			PortletObject pobj = PortletObject.Read(obj.PortletID);
			Control control = Builder.LoadPortletControl(pobj);
			AddNavigation(control.GetType());
			CreatePorletResource(obj.PagePortletID, pobj,control);
		}

		private void CreatePorletResource(Guid id,PortletObject portletObject, Control portletControl) {
			Portlet portlet = portletControl as Portlet;
			if (portlet == null ) return;
			if (!portlet.NeedAuthentication) return;
			ResourceObject obj = ResourceObject.New();
			obj.ResourceID = Guid.NewGuid();
			obj.InnerResourceID = id.ToString();
			obj.ResourceType = "portlet";
			obj.ResourceName = portletObject.PortletName;
			//obj.ParentResourceID = EditPageID;
			obj.Save();
		}

		PageHierarchyBuilder builder;
		PageHierarchyBuilder Builder {
			get {
				if (builder == null) {
					builder = new PageHierarchyBuilder();
					builder.Page = Page;
					builder.Container = pageAreaHolder;
				}
				return builder;
			}
		}

		private void AddNavigation(Type ct) {
			NavigationPortletAttribute attr = (NavigationPortletAttribute)Attribute.GetCustomAttribute(ct, typeof(NavigationPortletAttribute));
			if (attr != null) {
				PortletNavigationObject obj;
				try {
					obj = PortletNavigationObject.Read(attr.NavigationName);
				} catch (CSObjectNotFoundException) {
					obj = PortletNavigationObject.New();
					obj.NavigationName = attr.NavigationName;
				}
				obj.PageID = EditPageID;
				obj.Save();
				PortletNavigateHttpModule.portletInTabDictionary.Remove(attr.NavigationName);
			}
		}

		void ModuleDropDownList_SelectedIndexChanged(object sender, EventArgs e) {
			BindPortlets();
		}
		void CreatePage_Load(object sender, EventArgs e) {
			string pageid = Request.QueryString["editpageid"];
			if (!string.IsNullOrEmpty(pageid)) {
				EditPageID = new Guid(pageid);
				CreateEditPage();
			} else {
				Navigate(typeof(ChooseLayout));
				return;
			}
			if (!IsPostBack && !EditMode) {
				BindModules();
				BindPortlets();
				BindPanes();
			}
		}


		private void BindPanes() {
			ddlPanes.Items.Clear();
			PageObject page = PageObject.Read(EditPageID);
			Control c = Page.ParseControl(page.LayoutObject.LayouTemplate);
			List<string> list = PageHierarchyBuilder.GeneratePaneNameList(c);
			foreach (string str in list) {
				ListItem item = new ListItem(str, str);
				ddlPanes.Items.Add(item);
			}
		}

		private void BindPortlets() {
			string mid = ddlModules.SelectedValue;
			CSList<PortletObject> list = PortletObject.List("ModuleID=@ModuleID", "@ModuleID", new Guid(mid));
			ddlPortlets.DataSource = list;
			ddlPortlets.DataTextField = "PortletName";
			ddlPortlets.DataValueField = "PortletID";
			ddlPortlets.DataBind();
		}

		private void BindModules() {
			CSList<ModuleObject> list = ModuleObject.List();
			ddlModules.DataSource = list;
			ddlModules.DataTextField = "ModuleName";
			ddlModules.DataValueField = "ModuleID";
			ddlModules.DataBind();
		}
		Guid _editPageID = Guid.Empty;
		Guid EditPageID {
			get {
				return _editPageID;
			}
			set { _editPageID = value; }
		}

		private void CreateEditPage() {
			if (this.EditMode) return;
			pageAreaHolder.Controls.Clear();
			PageObject page = PageObject.Read(EditPageID);
			//PageHierarchyBuilder builder = new PageHierarchyBuilder(Page, page,pageAreaHolder);
			Builder.PageObject = page;
			Builder.EditMode = true;
			Builder.InitializePage();
		}

		protected override void OnPreRender(EventArgs e) {
			if (EditMode) {
				pageAreaHolder.BorderStyle = BorderStyle.None;
			}
			base.OnPreRender(e);
		}
	}
}